package com.ashlikun.core.activity

import android.content.Intent
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.ashlikun.core.R
import com.ashlikun.core.listener.BaseWindow
import com.ashlikun.core.listener.IBaseWindow
import com.ashlikun.core.listener.OnDispatcherMessage
import com.ashlikun.loadswitch.ContextData
import com.ashlikun.loadswitch.LoadSwitch
import com.ashlikun.loadswitch.LoadSwitchService
import com.ashlikun.supertoobar.SuperToolBar
import com.ashlikun.utils.ui.status.enableEdgeToEdgeX

/**
 * @author　　: 李坤
 * 创建时间: 2021.12.20 15:56
 * 邮箱　　：496546144@qq.com
 *
 * 功能介绍：自定义最顶层的Activity
 * @see IBaseWindow : 只要是窗口都会实现这个统一接口
 *
 * @see OnDispatcherMessage : Activity处理其他
 */

abstract class BaseActivity : IBaseActivity(), BaseWindow {


    //toolbar
    open val toolbar: SuperToolBar? by lazy {
        f(R.id.toolbar)
    }

    //布局切换
    override val switchService: LoadSwitchService? by lazy {
        if (switchRoot == null) null else LoadSwitch()
            .create(switchRoot!!, createSwitchLayoutListener())
    }

    //布局切换的根布局
    override val switchRoot: View? by lazy {
        f(R.id.switchRoot)
    }

    override fun baseInitView() {
        super.baseInitView()
        initLoadSwitch()
    }

    /**
     * 初始化布局切换的管理器
     */
    override fun initLoadSwitch() {
        val view = switchRoot
        if (view != null) {
            //这里得初始化
            switchService
        }
    }

    /**
     * ：解析意图
     */
    override fun parseIntent(intent: Intent) {
        runCatching {
            ARouter.getInstance().inject(this)
        }
    }


    override fun showLoading(data: ContextData, isForce: Boolean) {
        if (isForce) {
            switchService?.isLoadingCanShow = true
        }
        switchService?.showLoading(data)
    }


    override fun showContent() {
        switchService?.showContent()
    }

    override fun showEmpty(data: ContextData) {
        switchService?.showEmpty(data)
    }

    override fun showRetry(data: ContextData) {
        switchService?.showRetry(data)
    }


}